/**
 * @file data.h
 * @author YangSL (dalin@main.com)
 * @brief 
 * @version 0.0.1
 * @date 2022-09-28
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#ifndef   _DATA_H_
#define   _DATA_H_

#include "basetype.h"
#include "obj_data.h"

#pragma pack(1)


typedef struct 
{
    int16_t index;
    int8_t subindex;
}sindex;


/* 测试变量用例 */
typedef union
{
	uint32 	test32;
	uint16	test16[2];
	uint8	test8[4];
}test_data1;

typedef struct
{
	uint32 data1;
	uint32 data2;
	uint32 data3;
	uint32 data4;
}test_data2;

typedef struct
{
	uint8 data[7*10];
}test_data3;


typedef struct
{
	uint8 data[20];
}test_data4;
/* 传输状态控制 */
typedef struct
{
	/* 段传输 */
	sindex 	m_index;  				//记录索引
	sSDO_OBJ *sdo_p;		   		//记录SDO块对象

	int8_t	use_pre_state;			//使用前一次状态标志
	int8_t	cur_state;				//当前状态
	int8_t	pre_state;				//前一次状态
	int8_t	later_state;			//下一次状态

	uint8	blk_load_state;			//块下载状态

}state;

typedef union
{
	int8_t  	sdo_blk_buff[518];
	struct
	{
		uint16  crc16;
		uint16  size;
		uint16  reserve;
		int8_t  buf[512];
	}data;
	
}blk_buf;

typedef union
{
	uint8		u8[4];
	uint16		crc16[2];
	uint32		crc32;
}blk_crc;

typedef struct 	/* CAN通信数据 */
{
	test_data1 	test1;
	test_data2 	test2;
	test_data3  test3;
	test_data4  test4;

	state		state;
	int8_t  	sdo_buff[896];
	uint16		sdo_index;

	blk_buf  	blk_buff;				// 子块缓存
	uint16		sdo_blk_index;			// 子块索引
	uint32		sdo_blk_size;			// 块大小（字节）
	uint16		sdo_blk_num;			// 子块总数
	uint16		sdo_blk_cnt;			// 第几个子块
	uint16		sdo_blk_child_size;		// 子块大小

	blk_crc 	blkcrc;					//块CRC

	uint8_t		m_stop_cmd;				//终止命令
	uint8_t 	m_ack_state;			//客户端等待ACK标志
	uint8_t 	segment_nums;			//段编号
	uint8_t		remain_nums	;			//最后一段字节数

	uint32_t 	ticks;	
}ucom_data;		/* CAN通信数据 */

extern uint8 mc_pdo_data[100];
extern ucom_data com_data;
extern int pdo_period ;

#pragma pack()

/* 测试回调1 */
uint8 test1_write_CanSlot(void * sender);
/* 测试回调2 */
uint8 test2_write_CanSlot(void * sender);
/* 测试回调3 */
uint8 test3_write_CanSlot(void * sender);
uint8 test4_read_CanSlot(void * sender);
uint8 test5_read_CanSlot(void * sender);
uint8 test6_read_CanSlot(void * sender);
uint8 test7_write_CanSlot(void * sender);

/*PDO写*/
uint8 mc_pdo_write_01(void* sender);
uint8 mc_pdo_write_02(void* sender);
uint8 mc_pdo_write_03(void* sender);
uint8 mc_pdo_write_04(void* sender);
uint8 mc_pdo_write_05(void* sender);
uint8 mc_pdo_write_06(void* sender);
uint8 mc_pdo_write_07(void* sender);
uint8 mc_pdo_write_08(void* sender);
uint8 mc_pdo_write_09(void* sender);
uint8 mc_pdo_write_10(void* sender);
/**
 * @brief 初始化obj缓存为0
 * 
 */
void can_obj_init(void);

#endif

